package com.jrx.anytxn.transaction.job.coupon.config;

import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.param.entity.PrActivityInfo;
import com.jrx.anytxn.param.service.rights.IPrActivityInfoService;
import com.jrx.anytxn.transaction.config.CouponConfig;
import com.jrx.anytxn.transaction.service.IActivityLoanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;

/**
 * ClassName CouponHalfActivityStoreQuantityWeekendConfiguration
 *
 * @author lujianyou
 * @version 1.0
 * @description 五折过周末活动库存设置
 * @date 2021/11/4 11:00
 **/
@Configuration
public class CouponHalfActivityStoreQuantityWeekendConfiguration {

    private static final Logger logger =
        LoggerFactory.getLogger(CouponHalfActivityStoreQuantityWeekendConfiguration.class);
    private static final BigDecimal SUM_AMOUNT = new BigDecimal("299");
    private static final String TENANT_ID = "101";

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private IActivityLoanService activityLoanService;
    @Autowired
    private IPrActivityInfoService prActivityInfoService;
    @Autowired
    private CouponConfig couponConfig;

    @Bean
    public Job halfActivityStoreQuantityWeekendJob() {
        return jobBuilderFactory.get("halfActivityStoreQuantityWeekendJob")
            .start(halfActivityStoreQuantityWeekendStep()).build();
    }

    @Bean
    public Step halfActivityStoreQuantityWeekendStep() {
        return stepBuilderFactory.get("halfActivityStoreQuantityWeekendStep")
            .tasklet(halfActivityStoreQuantityWeekendTasklet()).build();
    }

    /**
     * 步骤： 1、统计当前周一至周四消费总额 2、查找库存设置比例 3、更新库存
     * 
     * @return
     */
    @Bean
    @StepScope
    public Tasklet halfActivityStoreQuantityWeekendTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("access into halfActivityStoreQuantityWeekendTasklet");
            String halfWeekendActivityId = couponConfig.getActivity().getHalfWeekendActivityId();;
            PrActivityInfo prActivityInfo =
                prActivityInfoService.getByTenantIdAndActivityId(TENANT_ID, halfWeekendActivityId);
            if (prActivityInfo == null) {
                logger.warn("根据tenantId:{}、activityId:{} 查无数据", TENANT_ID, halfWeekendActivityId);
                throw new TxnBizException(TxnRespCode.ACTIVITY_NOT_EXIST);
            }

            Integer ratio = prActivityInfo.getStoreQuantityRatio();
            if (ratio == null) {
                logger.warn("活动库存比例不能为空");
                throw new TxnBizException(TxnRespCode.ACTIVITY_STOCK_RATIO_NONE);
            }
            Integer sum = activityLoanService.halfActivityWeekend(TENANT_ID, SUM_AMOUNT);
            Double quantity = Math.ceil(ratio / 100D * sum);

            PrActivityInfo prActivityInfoToUpdate = new PrActivityInfo();
            prActivityInfoToUpdate.setTenantId(TENANT_ID);
            prActivityInfoToUpdate.setId(prActivityInfo.getId());
            prActivityInfoToUpdate.setStoreQuantity(quantity.intValue());
            prActivityInfoToUpdate.setRecordVersionNumber(prActivityInfo.getRecordVersionNumber() + 1);

            prActivityInfoService.updateByPrimaryKeySelective(prActivityInfoToUpdate);
            return RepeatStatus.FINISHED;
        };
    }
}
